#include "Tour.h"
#include "Dame.h"
#include "Roi.h"

#include <string>

using std::string;

string Tour::nom("Tour");

Tour::Tour(Echiquier * E, Position P, bool couleur, bool d):Piece(E, P, couleur),deplace(d)
{
	object = new  TourGL (this, couleur) ;
}
Tour::~Tour(){
	
	if (object){
		delete object;
		object = NULL ;
	}	
}

char Tour::rendu() const
{
	return (c?'T':'t');
}

const string& Tour::toString()const
{
	return nom;
}

void Tour::roque(const Roi* roi, bool type)
{
	Position newPos(
		(c ?
			(p == Position(7,0) ?
				Position(7,2) :
				(p == Position(7,7) ?
					Position(7,4) :
					p)
				) :
			(p == Position(0,0) ?
				Position(0,2) :
				(p == Position(0,7) ?
					Position(0,4) :
					p)
				)
			));
			
	std::cout << "***************************\n" << __PRETTY_FUNCTION__ << std::endl;
	
	if(!roi)
		return;
	
	std::cout << "test Roque" << std::endl;
	if(roi->roquePossible(type) && roi->estBlanc() == c)
	{
		std::cout << "peut Roquer" << std::endl;
		p = newPos;
		deplace = true;
	}
}

bool Tour::estDeplace()const
{
	return deplace;
}

void Tour::deplacement(const Position &p) throw (std::invalid_argument)
{
	Piece::deplacement(p);
	deplace = true;
}

void Tour::majAtk()
{
    if(typeid(*this) != typeid(Dame))
        atk.clear();
	bool cont(true);
	Position tmp(p);

	while(--tmp.getC() >= 0 && cont)
	{
			atk.push_back(tmp);
			cont = !e->getPiece(tmp);
	}

	cont = true; tmp = p;
	while(++tmp.getC() < 8 && cont)
	{
		atk.push_back(tmp);
		cont = !e->getPiece(tmp);
	}

	cont = true; tmp = p;
	while(--tmp.getL() >= 0 && cont)
	{
		atk.push_back(tmp);
		cont = !e->getPiece(tmp);
	}

	cont = true; tmp = p;
	while(++tmp.getL() < 8 && cont)
	{
		atk.push_back(tmp);
		cont = !e->getPiece(tmp);
	}
}

void Tour::majDeplacements()
{
    cloue = false;
	bool cont(true);
	Position tmp(p);

	if(typeid(*this) != typeid(Dame))
		dep.clear();

	while(--tmp.getC() >= 0 && cont)
		if((cont = !e->getPiece(tmp)) || (e->getPiece(tmp)->estBlanc() != estBlanc()))
			dep.push_back(tmp);

	cont = true; tmp = p;
	while(++tmp.getC() < 8 && cont)
		if((cont = !e->getPiece(tmp)) || (e->getPiece(tmp)->estBlanc() != estBlanc()))
			dep.push_back(tmp);

	cont = true; tmp = p;
	while(--tmp.getL() >= 0 && cont)
		if((cont = !e->getPiece(tmp)) || (e->getPiece(tmp)->estBlanc() != estBlanc()))
			dep.push_back(tmp);

	cont = true; tmp = p;
	while(++tmp.getL() < 8 && cont)
		if((cont = !e->getPiece(tmp)) || (e->getPiece(tmp)->estBlanc() != estBlanc()))
			dep.push_back(tmp);
}

Piece* Tour::clone(Echiquier* ne) const
{
    Piece* clone = new Tour(ne,p,c,deplace);
	clone->getDep() = dep;
	clone->getAtk() = atk;
	clone->getpAtk() = pAtk;
	clone->getpDef() = pDef;
	return clone;
}
